<include file="Public/popHeader"/>
<body class="hold-transition skin-blue sidebar-mini">
<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <div class="box-header">
                <h3 class="box-title">{$local_name}</h3>
                <div class="form-inline pull-right">
                    <form action="{:U('Promotion/getProducts',array('type_id'=>23,'order_field'=>'product_id','order'=>$order), '')}"
                          method="get" pjax-container="">
                        <fieldset>
                            <select class="form-control" name="search_field" style="height: 30px; font-size: 12px;">
                                <option value="name"
                                <if condition="$search_field eq 'name'">selected="selected"</if>
                                >货品名称</option>
                                <option value="barcode"
                                <if condition="$search_field eq 'barcode'">selected="selected"</if>
                                >条码</option>
                                <option value="bn"
                                <if condition="$search_field eq 'bn'">selected="selected"</if>
                                >BN</option>
                            </select>
                            <div class="input-group input-group-sm">
                                <div class="input-group input-group-sm">
                                    <input type="text" class="form-control" placeholder="" name="search"
                                           value="{$search}">
                                </div>
                            </div>

                            <div class="input-group input-group-sm">
                                <div class="input-group-btn">
                                    <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i>
                                    </button>
                                </div>
                            </div>
                        </fieldset>
                    </form>
                </div>

            </div>
            <!-- /.box-header -->
            <div class="box-body table-responsive no-padding">
                <table class="table table-hover">
                    <tbody>
                    <p style="color: red">请选择同一个供应商的商品</p>
                    <tr><input type="checkbox" class="grid-select-all"><th>
                    </th>
                        <th>货品ID<a class="fa fa-fw fa-sort"
                                   href="{:U('Special/getProducts',array('p'=>$p,'order_field'=>'product_id','order'=>$order), '')}"></a>
                        </th>
                        <th>商品ID<a class="fa fa-fw fa-sort"
                                   href="{:U('Special/getProducts',array('p'=>$p,'order_field'=>'goods_id','order'=>$order), '')}"></a>
                        </th>
                        <th>供应商</th>
                        <th>名称</th>
                        <th>商品分类</th>
                        <th>图片</th>
                        <th>促销活动</th>
                        <th>条码<a class="fa fa-fw fa-sort"
                                 href="{:U('Special/getProducts',array('p'=>$p,'order_field'=>'barcode','order'=>$order), '')}"></a>
                        </th>
                        <th>BN<a class="fa fa-fw fa-sort"
                                 href="{:U('Special/getProducts',array('p'=>$p,'order_field'=>'bn','order'=>$order), '')}"></a>
                        </th>
                        <th>销售价<a class="fa fa-fw fa-sort"
                                  href="{:U('Special/getProducts',array('p'=>$p,'order_field'=>'price','order'=>$order), '')}"></a></th>
                        <th>市场价<a class="fa fa-fw fa-sort"
                                  href="{:U('Special/getProducts',array('p'=>$p,'order_field'=>'marketprice','order'=>$order), '')}"></a></th>
                        <th>折后价<a class="fa fa-fw fa-sort"
                                  href="{:U('Special/getProducts',array('p'=>$p,'order_field'=>'discount','order'=>$order), '')}"></a></th>
                        <th>库存<a class="fa fa-fw fa-sort"
                                 href="{:U('Special/getProducts',array('p'=>$p,'order_field'=>'store','order'=>$order), '')}"></a></th>
                    </tr>
                    <volist name="list" id="vo">
                        <tr class="cg-product-tr"
                        <?php if (checkProductIsGroupPromotion($vo['store_pid'], $vo['local_id']) || goods_is_Goods_promotion($vo['store_pid'], $vo['local_id']) || goods_is_shangou($vo['store_pid'], $vo['local_id'])){
                                            echo "style='text-decoration:line-through'";
                                        }
                                        ?>
                        >
                        <td>
                            <input type="checkbox"
                            <?php if (checkProductIsGroupPromotion($vo['store_pid'], $vo['local_id']) || goods_is_Goods_promotion($vo['store_pid'], $vo['local_id']) || goods_is_shangou($vo['store_pid'], $vo['local_id'])){
                                echo "disabled='disabled'";
                                }else{
                                    echo 'class="grid-item"';
                                }
                                ?>
                            data-price="{$vo.price}"
                            data-id="{$vo.store_pid}" >
                        </td>

                        <td>{$vo.product_id}</td>
                        <td>{$vo.goods_id}</td>
                        <td>{$vo.name}</td>
                        <td>{$vo.cat_name}</td>
                        <td><img style="width: 80px;" src="{$vo.goods_id|getThumb=###}" alt=""></td>
                        <td>
                            <?php
                            if (goods_is_Order_promotion($vo['store_pid'], $vo['local_id'])) {
                                                echo '<span class="badge bg-green">订单促销</span>';
                            }
                            if (goods_is_Goods_promotion($vo['store_pid'], $vo['local_id'])) {
                            echo '<span class="badge bg-green">商品促销</span>';
                            }
                            if (goods_is_shangou($vo['store_pid'], $vo['local_id'])) {
                            echo '<span class="badge bg-green">闪购</span>';
                            }

                            if (checkProductIsGroupPromotion($vo['store_pid'], $vo['local_id'])) {
                            echo '<span class="badge bg-green">组合促销</span>';
                            }
                            if (checkProductIsSpecial($vo['store_pid'], $vo['local_id'])) {
                            echo '<span class="badge bg-green">专题商品包</span>';
                            }
                            ?>
                        </td>
                        <td>{$vo.barcode}</td>
                        <td>{$vo.bn}</td>
                        <td>{$vo.price}</td>
                        <td>{$vo.marketprice}</td>
                        <td>{$vo.discount}</td>
                        <td>{$vo.store}</td>
                        </tr>
                    </volist>
                    </tbody>
                </table>
            </div>
            <div class="box-footer clearfix">
                <input type="checkbox" class="grid-select-all">&nbsp;&nbsp;&nbsp;
                <a class="btn btn-sm btn-info batch-choose">保存</a>
                <a class="btn btn-sm btn-primary grid-refresh"><i class="fa fa-refresh"></i></a>
                {$page}
            </div>
            <!-- /.box-body -->
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        $('.grid-select-all').change(function () {
            if (this.checked) {
                $('.grid-item').prop("checked", true);
            } else {
                $('.grid-item').prop("checked", false);
            }
        });
        $('.grid-refresh').click(function () {
            window.location.reload();
        });

        var chids = parent.$("#yc-goods-ids").val();
        var ychser = new YcChoser();
        ychser.setChosenIds(chids);

        $(".grid-item").each(function(){
            if(ychser.isRepeat($(this).attr("data-id"))){
                $(this).prop("checked", "checked");
            }
        });

        $('.batch-choose').on('click', function (e) {
            /*if (!confirm("是否继续")) {
             return false;
             }*/
            var data = [];
            var exists = parent.$('.ui-sortable').find('li input');
            var total_price = parent.$('input[name=total_price]').val();
            var supplier_id_data = [];
            var html = '';

            $('input[name="test"]:checked').each(function(i){
                supplier_id_data[i] = $(this).attr('supplier_id');
            });

            console.log(supplier_id_data);
            for ($j=0;$j<supplier_id_data.length;$j++){
                $a = supplier_id_data[0];
                if ($a != supplier_id_data[$j]){
                    alert('请选择相同的供应商');
                    return;
                }
            }


            $(".grid-item").each(function (i) {

                var store_pid = $(this).attr('data-id');
                var supplierid =  $('input[name="test"]:checked').attr('supplier_id');
                if(supplierid==''){
                    supplierid=0;
                }
                var price = $(this).attr('data-price');
                var product_id = $(this).parent().parent().find('td:eq(1)').text();
                var name = $(this).parent().parent().find('td:eq(4)').text();
//                var price = $(this).parent().parent().find('td:eq(10)').text();
                var store = $(this).parent().parent().find('td:eq(13)').text();
                var flag = ychser.isRepeat($(this).attr("data-id"));
                if(flag && !$(this).is(':checked')){
                    ychser.remove();
                    parent.$('.ui-sortable').children("li").eq(ychser.getPosition()).remove();
                }else if ($(this).is(':checked') && !flag) {
                    ychser.add($(this).attr("data-id"));

                    html += '<li class="one-product">';
                    html += '<input type="hidden" name="sp_id[]" value="'+store_pid+'">';
                    html += '<input type="hidden" name="product_id[]" value="'+product_id+'">';
                    html += '<input type="hidden" class="supplier_id" name="supplier_id[]" value="'+supplierid+'">';
                    html += '<a href="javascript:;" title="'+name+'" class="text ebatch-name">'+name+'</a>';
                    html += '<small class="label label-warning"> 售价 : '+price+'</small>';
                    html += '<small class="label label-success"> 库存 : '+store+'</small>';
                    html += ' 货品数量 : <span class="text"><input type="text" class="geshu" name="num[]" data-num="'+store+'" data-price="'+price+'" value="1" style="width:50px;"></span>';
                    html += '<div class="tools">';
                    html += '<i class="fa fa-trash-o icondel" data-id="'+store_pid+'" onclick="delGoodsItem(this)"></i>';
                    html += '</div>';
                    html += '</li>';
                }

            });
            parent.$('.ui-sortable').append(html);
            parent.updateGoodsIds(ychser.getChosenIds());
            alert('成功');
            //parent.layer.close(index);
        })
    });
</script>
</body>
</html>